package demo.fx;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhuzi
 * 2022/8/18 00:01
 */
public class Animal {

    public int countLegs() {
        return 0;
    }

    private static class Dog extends Animal {

        @Override
        public int countLegs() {
            return 1;
        }
    }

    private static class Cat extends Animal {

        @Override
        public int countLegs() {
            return 2;
        }
    }

    static int countLegs(List<? extends Animal> animals) {
        int retVal = 0;
        for (Animal animal : animals) {
            retVal += animal.countLegs();
        }
        return retVal;
    }

    static int countLegs1(List<Animal> animals) {
        int retVal = 0;
        for (Animal animal : animals) {
            retVal += animal.countLegs();
        }
        return retVal;
    }

    public static void main(String[] args) {
        List<Dog> dogs = new ArrayList<>();
        // 不会报错
        countLegs(dogs);
        // 报错
        //countLegs1(dogs);
    }
}
